package com.example.sunlight.interneteverything.View;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.example.sunlight.interneteverything.R;

/**
 * Created by SunLight on 2017/8/8.
 */

public class HealthRunView extends View {
    Bitmap one, two;
    Resources resources;
    SizeChange sizeChange;
    UpDown upDown;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0x123:
                    HealthRunView.this.invalidate();
                    break;
            }
        }
    };

    public HealthRunView(Context context) {
        super(context);
        init();
        sizeChange = new SizeChange();
        sizeChange.start();
        upDown = new UpDown();
        upDown.start();
    }

    public void init() {
        resources = getResources();
        one = ((BitmapDrawable) resources.getDrawable(R.drawable.run)).getBitmap();
        two = ((BitmapDrawable) resources.getDrawable(R.drawable.man)).getBitmap();
    }

    public HealthRunView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public HealthRunView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void drawRotateBitmap(Canvas canvas, Paint paint, Bitmap bitmap, float rotation, float posX, float posY) {
        Matrix matrix = new Matrix();
        int offsetX = bitmap.getWidth() / 2;
        int offsetY = bitmap.getHeight() / 2;
        matrix.postTranslate(-offsetX, -offsetY);
        matrix.postRotate(rotation);
//        matrix.postScale(1f, 0.8f);
        matrix.postTranslate(posX + offsetX, posY + offsetY);
        canvas.drawBitmap(bitmap, matrix, paint);
    }

    int kit1, kit2, kit3, kit4;
    int size1, size2, size3, size4;
    int num1 = 150, num2 = 150, num3 = 150, num4 = 150;
    boolean sate2, sate3, sate4;

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        drawRotateBitmap(canvas, null, two, 0, 215, 155 + size);

        canvas.translate((getWidth() + getPaddingLeft() - getPaddingRight()) / 2-10,
                (getHeight() + getPaddingTop() - getPaddingBottom()) / 2);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStrokeWidth(20);

        kit1 = Color.argb(num1, 0, 163, 255);
        paint.setColor(kit1);
        canvas.drawCircle(0, 0, 40 + size1, paint);

        kit2 = Color.argb(num2, 0, 163, 255);
        paint.setColor(kit2);
        canvas.drawCircle(0, 0, 40 + size2, paint);

        kit3 = Color.argb(num3, 0, 163, 255);
        paint.setColor(kit3);
        canvas.drawCircle(0, 0, 40 + size3, paint);

        kit4 = Color.argb(num4, 0, 163, 255);
        paint.setColor(kit4);
        canvas.drawCircle(0, 0, 40 + size4, paint);

    }

    boolean sate = false;
    int size = 0;

    class UpDown extends Thread {
        @Override
        public void run() {
            try {
                while (true) {
                    Thread.sleep(45);
                    if (sate == false) {
                        if (size <= 5) {
                            size++;
                        } else {
                            sate = true;
                        }
                    } else {
                        if (size >= 0) {
                            size--;
                        } else {
                            sate = false;
                        }
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    class SizeChange extends Thread {
        @Override
        public void run() {
            try {
                while (true) {
                    Thread.sleep(45);
                    if (size1 < 170) {
                        size1 += 2;
                        num1 -= 1;
                        if (size1 >= 80) {
                            sate2 = true;
                        }
                    } else {
                        if (num1 != 0) {
                            num1--;
                        } else {
                            size1 = 0;
                            num1 = 150;
                        }
                    }

                    if (sate2) {
                        if (size2 < 170) {
                            size2 += 2;
                            num2 -= 1;
                            if (size2 >= 80) {
                                sate3 = true;
                            }
                        } else {
                            if (num2 != 0) {
                                num2--;
                            } else {
                                size2 = 0;
                                num2 = 150;
                            }
                        }
                    }
                    if (sate3) {
                        if (size3 < 170) {
                            size3 += 2;
                            num3 -= 1;
                            if (size3 >= 80) {
                                sate4 = true;
                            }
                        } else {
                            if (num3 != 0) {
                                num3--;
                            } else {
                                size3 = 0;
                                num3 = 150;
                            }
                        }
                    }
                    if (sate4) {
                        if (size4 < 170) {
                            size4 += 2;
                            num4 -= 1;
                            if (size4 >= 80) {
                                sate4 = true;
                            }
                        } else {
                            if (num4 != 0) {
                                num4--;
                            } else {
                                size4 = 0;
                                num4 = 150;
                            }
                        }
                    }
                    handler.sendEmptyMessage(0x123);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }


        }
    }
}
